﻿using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace TimerTask.Common.Utils
{
    public static class ObjExtension
    {
        /// <summary>
        /// Object序列化为String
        /// author:肖阳
        /// 2016-09-24 17:02:58
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToJson(this object o)
        {
            return JsonConvert.SerializeObject(o);
        }


        /// <summary>
        /// Json反序列化为实体
        /// author:肖阳
        /// 2016-09-24 17:02:33
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ToObj<T>(this string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }


        /// <summary>
        /// 是否为Null
        /// author:肖阳
        /// 2016-09-24 17:13:32
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static bool IsNull(this object o)
        {
            return o == null;
        }


        /// <summary>
        /// 将对象转成dic对象
        /// author:肖阳
        /// date:2016-10-22 08:18:36
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ToDic(this object o)
        {
            return o.ToJson().ToObj<Dictionary<string, object>>();
        }
    }
}
